package com.blacksheep.mtm.search.message;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.blacksheep.mtm.dao.TbMedicineMapper;
import com.blacksheep.mtm.dao.TbMemberMapper;
import com.blacksheep.mtm.pojo.DemoData;
import com.blacksheep.mtm.pojo.manager.TbMedicine;
import com.blacksheep.mtm.pojo.manager.TbMember;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class DemoDataListener extends AnalysisEventListener<DemoData> {

    List<DemoData> list = new ArrayList<DemoData>();
    private TbMedicineMapper medicineMapper;
    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     *
     * @param medicineMapper
     */
    public DemoDataListener(TbMedicineMapper medicineMapper) {
        this.medicineMapper = medicineMapper;
    }
    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data
     *            one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(DemoData data, AnalysisContext context) {
        list.add(data);
        System.out.println(data.toString());
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
    }
    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
    }
    /**
     * 加上存储数据库
     */
    private void saveData() {

    }
}
